package cn.bx.consumer.service;

import cn.bx.consumer.util.StringUtil;
import org.example.pojo.AjaxResult;
import org.example.pojo.Page;
import org.example.pojo.User;
import org.example.service.UserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;

@Service
public class UserService implements UserServiceI {

    /*@Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;*/

    @Autowired
    private UserFeignClient userFeignClient;

    public AjaxResult userGet(long id) {
        return userFeignClient.userGet(id);
    }

    /*public AjaxResult userGet(long id){
        List<ServiceInstance> bids = discoveryClient.getInstances("bid");
        ServiceInstance bid=bids.get(0);
        //String url="http://localhost:9999/userGet?id="+id;
        String url="http://"+bid.getHost()+":"+bid.getPort()+"/userGet?id="+id;
        System.out.println(">>>>>"+url);
        return restTemplate.getForObject(url,AjaxResult.class);
    }*/

    @Override
    public AjaxResult login(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        System.out.println(user);
        return userFeignClient.login(user);
    }

    /*public AjaxResult login1(String username,String password){
        List<ServiceInstance> bids = discoveryClient.getInstances("bid");
        ServiceInstance bid=bids.get(0);
        String url="http://"+bid.getHost()+":"+bid.getPort()+"/login1";
        User u=new User();
        u.setUsername(username);
        u.setPassword(password);
        System.out.println(">>>>>"+url);
        return restTemplate.postForObject(url,u,AjaxResult.class);
    }*/

    public Page<User> userSearch(int pageNo, int limit, String name, Date start, Date end) {
        return userFeignClient.userSearch(pageNo, limit, name, StringUtil.formatDate(start), StringUtil.formatDate(end));
    }

    /*@Override
    public Page<User> userSearch(int pageNo, int limit, String name, Date start, Date end) {
        List<ServiceInstance> bids = discoveryClient.getInstances("bid");
        ServiceInstance bid = bids.get(0);
        //String url="http://localhost:8880/userSearch?page="+page+"&limit="+limit+"&name="+name+"&start="+start+"&end="+end;
        String url="http://"+bid.getHost()+":"+bid.getPort()+"/userSearch?";
        StringBuilder sb=new StringBuilder();
        sb.append("?");
        sb.append("page="+pageNo);
        sb.append("limit="+limit);
        if(StringUtil.isBlank(name)==false){
            sb.append("&name"+name);
        }
        if(start!=null)
            sb.append("&start="+ StringUtil.formatDate(start));

        sb.append("&end="+ StringUtil.formatDate(end));
        return restTemplate.getForObject(url+sb,Page.class);
    }*/
    @Override
    public AjaxResult userAdd(User user) {
        return userFeignClient.userAdd(user);
    }

    @Override
    public AjaxResult userUpdate(User user) {
        return userFeignClient.userUpdate(user);
    }

    @Override
    public AjaxResult del(long id) {
        return userFeignClient.del(id);
    }

    @Override
    public AjaxResult batchDel(long[] ids) {
        return userFeignClient.batchDel(ids);
    }

    @Override
    public AjaxResult checkUserNameRepeat(String userName, Long id) {
        return userFeignClient.checkUserNameRepeat(userName, id);
    }
}
